package com.jy.service.filemanager.histask;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jy.entity.filemanager.TSFile;
import com.jy.entity.filemanager.TSHitask;
import com.jy.entity.filemanager.TSTaskStatistic;
import com.jy.repository.filemanager.HisTaskDao;
import com.jy.service.base.BaseServiceImp;

@Service("hisTaskService")
public class HisTaskServiceImp extends BaseServiceImp<TSHitask> implements
		HisTaskService {

	/** 调度工厂Bean */
	@Autowired
	private Scheduler scheduler;

	@Override
	@Transactional
	public int creatHisTask(TSHitask o) {
		int res = 0;
		try {
			o.setCreateDate(new Timestamp(System.currentTimeMillis()));
			// 更新数据库
			super.insert(o);
			res = 1;
		} catch (Exception e) {
			logger.error("创建任务失败", e);
		}
		return res;
	}

	@Override
	@Transactional
	public int updateHisTask(TSHitask o) {
		int res = 0;
		try {
			HisTaskDao dao = (HisTaskDao) baseDao;
			// 从数据库查找原信息
			dao.update(o);
			res = 1;
		} catch (Exception e) {
			logger.error("创建任务失败", e);
		}
		return res;
	}

	@Override
	@Transactional
	public int deleteHisTask(TSHitask o) {
		int res = 0;
		try {
			HisTaskDao dao = (HisTaskDao) baseDao;
			// 从数据库查找原信息
			dao.deleteHisTask(o);
			res = 1;
		} catch (Exception e) {
			logger.error("删除任务失败", e);
		}
		return res;
	}

	@Override
	@Transactional
	public int saveTaskFile(TSFile o) {
		int res = 0;
		try {
			HisTaskDao dao = (HisTaskDao) baseDao;
			// 从数据库查找原信息
			dao.saveTaskFile(o);
			res = 1;
		} catch (Exception e) {
			logger.error("新增任务文件失败", e);
		}
		return res;
	}

	@Override
	@Transactional
	public List<TSFile> findFileByHisTaskId(TSHitask o) {
		List<TSFile> list = new ArrayList<TSFile>();
		try {
			HisTaskDao dao = (HisTaskDao) baseDao;
			list = dao.findFileByHisTaskId(o.getId());
		} catch (Exception e) {
			logger.error("新增任务文件失败", e);
		}
		return list;
	}

	@Override
	@Transactional
	public int findTaskNum(String type) {
		int res = 0;
		try {
			HisTaskDao dao = (HisTaskDao) baseDao;
			res = dao.findTaskNum(type);
		} catch (Exception e) {
			logger.error("新增任务文件失败", e);
		}
		return res;
	}

	@Override
	@Transactional
	public int findTaskFileNum(String type) {
		int res = 0;
		try {
			HisTaskDao dao = (HisTaskDao) baseDao;
			res = dao.findTaskFileNum(type);
		} catch (Exception e) {
			logger.error("新增任务文件失败", e);
		}
		return res;
	}

	@Override
	@Transactional
	public int findTaskProblemNum(String type) {
		int res = 0;
		try {
			HisTaskDao dao = (HisTaskDao) baseDao;
			res = dao.findTaskProblemNum(type);
		} catch (Exception e) {
			logger.error("新增任务文件失败", e);
		}
		return res;
	}

	@Override
	@Transactional
	public List<TSTaskStatistic> findTaskStaticInfo(TSTaskStatistic o) {
		List<TSTaskStatistic> list = new ArrayList<TSTaskStatistic>();
		try {
			HisTaskDao dao = (HisTaskDao) baseDao;
			list = dao.findTaskStaticInfo(o);
		} catch (Exception e) {
			logger.error("查询任务统计失败", e);
		}
		return list;
	}

}
